﻿using AutoMapper;
using B.S.BasicData.Api.Read.Application.Command.Approval;
using B.S.BasicData.Api.Read.DTOS;
using B.S.BasicData.Domain.Entity.Approval;
using B.S.BasicData.ErrorCode;
using B.S.BasicData.Infastructure.Interface;
using MediatR;

namespace B.S.BasicData.Api.Read.Application.Handler.Approval
{
    public class GetLeaveTypeHandler : IRequestHandler<GetLeaveTypeCommand, ApiResult<List<LeaveTypeDto>>>
    {
        private readonly IBaseRepository<LeaveTypeModel> _leaveRepository;
        private readonly IMapper _mapper;

        public GetLeaveTypeHandler(IBaseRepository<LeaveTypeModel> leaveRepository, IMapper mapper)
        {
            _leaveRepository = leaveRepository;
            _mapper = mapper;
        }
        /// <summary>
        /// 获取请假类型
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<ApiResult<List<LeaveTypeDto>>> Handle(GetLeaveTypeCommand request, CancellationToken cancellationToken)
        {
            ApiResult<List<LeaveTypeDto>> result = new ApiResult<List<LeaveTypeDto>>();

            var leaves = _leaveRepository.GetAll();
            result.Code = StatusEnum.OK;
            result.Data=_mapper.Map<List<LeaveTypeDto>>(leaves);
            result.Message = "获取成功";
            return Task.FromResult(result);
        }
    }
}
